import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../components/home.vue'
import Login from '../components/account/login.vue'
import Goods from '../components/home/goos/index.vue'
import Search from '../components/search/index.vue'
import Cart from '../components/cart/index.vue'
import Order from '../components/order/index.vue'
import Address from '../components/address/index.vue'
import Navtive from '@/components/home/navtive/index.vue'
import Balance from '@/components/balance/index.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '',

    component: Navtive, // 主页

    children: [
      {
        path: '/login',

        component: Login
      },
      {
        path: '/search',

        component: Search
      },
      {
        path: '/cart',

        component: Cart
      },
      {
        path: '/order',

        component: Order
      },
      {
        path: '/address',

        component: Address
      },
      {
        path: '/balance',

        component: Balance
      },
      { path: '/', redirect: '/home', component: Home }, // 根路径
      {
        path: '/home',

        component: Home, // 主页
        children: [

          { path: '/home', component: Goods }
        ]

      }
    ]
  }
]

const router = new VueRouter({
  routes,
  mode: 'history'
})

// 挂载路由导航守卫,to表示将要访问的路径，from表示从哪里来，next是下一个要做的操作 next('/login')强制跳转login
router.beforeEach((to, from, next) => {
  // 访问登录页，放行
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  // 没有token, 强制跳转到登录页
  if (!tokenStr) return next('/login')
  next()
})

export default router
